package mongodb

import (
	"sync"

	"gopkg.in/mgo.v2/bson"
)

type DBExecInfo struct {
	ActType    int
	Collection string // 表名
	Cond       bson.M // 条件
	Doc        bson.M
	Skip       int
	Limit      int

	P1       interface{}
	Result   []bson.M
	Err      error
	CallFunc func(*DBExecInfo)
}

func (m *DBExecInfo) Reset() {
	m.ActType = 0
	m.Collection = ""
	m.Cond = nil
	m.Doc = nil
	m.Skip = 0
	m.Limit = 0

	m.P1 = nil
	m.Result = nil
	m.Err = nil
	m.CallFunc = nil
}

var (
	pool sync.Pool
)

func init() {
	pool = sync.Pool{
		New: func() interface{} {
			return &DBExecInfo{}
		},
	}
}

func GetDBExecInfo(acttype int, coll string, cb func(*DBExecInfo)) *DBExecInfo {
	p := pool.Get().(*DBExecInfo)
	p.ActType = acttype
	p.Collection = coll
	p.CallFunc = cb
	return p
}

func PutDBExecInfo(p *DBExecInfo) {
	p.Reset()
	pool.Put(p)
}
